using UnityEngine;

public class Checkpoint : MonoBehaviour
{
    private static readonly int Active = Animator.StringToHash("active");
    private Animator anim;
    public string id;
    public bool isActive;
    public Transform Locator { get; private set; }

    private void Awake()
    {
        anim = GetComponent<Animator>();
        Locator = transform.Find("Locator");
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.TryGetComponent(out Player _))
        {
            return;
        }
        ActivateCheckpoint();
    }

    public void ActivateCheckpoint(bool isActive = true)
    {
        if (this.isActive) return;
        AudioManager.Instance.PlaySfx(5, transform);
        this.isActive = isActive;
        anim.SetBool(Active, isActive);
    }

    [ContextMenu("Generate checkpoint ID")]
    private void GenerateId()
    {
        id = System.Guid.NewGuid().ToString();
    }
}
